//
//  BaseListViewController.m
//  CarDeal_IOS
//
//  Created by 孙可 on 2019/2/19.
//  Copyright © 2019年 sunke. All rights reserved.
//

#import "BaseListViewController.h"
#import "MJRefresh.h"

@interface BaseListViewController ()

@property(nonatomic,assign)BOOL isRefresh;
@property(nonatomic,assign)BOOL isLoadMore;

@end

@implementation BaseListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _array = [[NSMutableArray alloc] init];
    [self initViews];
    // Do any additional setup after loading the view.
}

-(void)initData{
    
}

-(void)initViews{
    _dataRyc = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [_dataRyc registerClass:[self getClass] forCellReuseIdentifier:[self getCellID]];
    _dataRyc.separatorStyle = UITableViewCellSeparatorStyleNone;
    _dataRyc.delegate = self;
    _dataRyc.dataSource = self;
    _dataRyc.emptyDataSetSource = self;
    _dataRyc.emptyDataSetDelegate = self;
    
    [self.view addSubview:_dataRyc];

    
    [self initData];

    [self makeConstraint];
    
    if([self isNeedRefresh]){
        _dataRyc.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            // 进入刷新状态后会自动调用这个block
            if(!_isRefresh){
                _isRefresh = YES;
                [self doRefreshData];
            }
        }];
    }
    
    [self getListData];
}

-(void)makeConstraint{
    
    [_dataRyc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

-(BOOL)isNeedRefresh{
    return YES;
}

-(BOOL)isNeedLoadMore{
    return YES;
}


-(UITableViewCell *)getCell{
    return [_dataRyc dequeueReusableCellWithIdentifier:[self getCellID]];
}

-(void)getListData{
    
}

-(void)doRefreshData{
    
}

-(void)doLoadData{
    
}

-(void)setCanLoadMore{
    if(_array.count>6){
        if([self isNeedLoadMore]){
            _dataRyc.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                if(!_isLoadMore){
                    _isLoadMore = YES;
                    [self doLoadData];
                }
            }];
        }
    }else{
        _dataRyc.mj_footer = nil;
    }

}

-(void)finishRefreshData:(NSArray *)dataArray{
    [self justIsLoading];
    [_array removeAllObjects];
    [_array addObjectsFromArray:dataArray];
    [_dataRyc reloadData];
    [self setCanLoadMore];
}

-(void)finishLoadMoreData:(NSArray *)dataArray{
    [self justIsLoading];
    if(dataArray == nil||dataArray.count == 0){
        [_dataRyc.mj_footer endRefreshingWithNoMoreData];
        return;
    }
    if(_isLoadMore){
        _isLoadMore = NO;
        [_dataRyc.mj_footer endRefreshing];
    }
    [_array addObjectsFromArray:dataArray];
    [_dataRyc reloadData];
    [self setCanLoadMore];
}

-(void)finishLoadData:(NSArray *)dataArray{
    [self justIsLoading];
    [_array removeAllObjects];
    [_array addObjectsFromArray:dataArray];
    [_dataRyc reloadData];
    [self setCanLoadMore];
}

-(void)justIsLoading{
    if(_isRefresh){
        _isRefresh = NO;
        [_dataRyc.mj_header endRefreshing];
    }
    if(_isLoadMore){
        _isLoadMore = NO;
        [_dataRyc.mj_footer endRefreshing];
    }
}

-(id)getObjectAtIndex:(NSIndexPath *)indexPath{
    return [_array objectAtIndex:indexPath.row];
}

-(NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *title = @"暂无数据哦!";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:[UIColor darkGrayColor]
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

@end
